package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.mappers.SmsCouponHistoryMapper;
import com.tanhua.model.domain.SmsCouponHistory;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsCouponHistoryApiImpl implements SmsCouponHistoryApi{

    @Autowired
    private SmsCouponHistoryMapper smsCouponHistoryMapper;

    /**
     * 根据coupon_id 查询优惠卷总数量 使用数量等等
     * use_status 0->未使用；1->已使用；2->已过期
     * @param coupon_id
     * @param use_status
     * @return
     */
    @Override
    public Integer findByCouponIdStatusCount(Long coupon_id, Integer use_status) {
        LambdaQueryWrapper<SmsCouponHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(coupon_id != null,SmsCouponHistory::getCouponId,coupon_id)
                .eq(use_status != null,SmsCouponHistory::getUseStatus,use_status);
        return smsCouponHistoryMapper.selectCount(lqw);
    }

    /**
     * 根据优惠券id，使用状态，订单编号分页获取领取记录
     * @param couponId
     * @param orderSn
     * @param useStatus
     * @param page
     * @return
     */
    @Override
    public Page couponHistoryList(Long couponId, String orderSn, Integer useStatus, Page<SmsCouponHistory> page) {
        LambdaQueryWrapper<SmsCouponHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(couponId != null,SmsCouponHistory::getCouponId,couponId)
                .like(orderSn != null,SmsCouponHistory::getOrderSn,orderSn)
                .eq(useStatus != null,SmsCouponHistory::getUseStatus,useStatus);
        return smsCouponHistoryMapper.selectPage(page,lqw);
    }
}
